package sy.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.OrderGoods;
import sy.service.OrderGoodsServiceI;

@Controller
@RequestMapping("/orderGoodsController")
public class OrderGoodsController extends BaseController {
	
	@Autowired
	private OrderGoodsServiceI orderGoodsService;
	
	@RequestMapping("/datagrid")
	@ResponseBody
	public DataGrid datagrid(OrderGoods order) throws Exception {
		return orderGoodsService.datagrid(order);
	}
	
	@RequestMapping("/datagrid/accepted")
	@ResponseBody
	public DataGrid datagridForAccepted(OrderGoods order) throws Exception {
		return orderGoodsService.datagridForAccepted(order);
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(OrderGoods order, HttpSession session) {
		Json j = new Json();
		try {
			OrderGoods result = orderGoodsService.add(order);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(result);
			
			//add log
			logUserAction(session, "/orderGoodsController/add", result.toString());
			
		} catch (Exception e) {
			j.setMsg("添加失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(OrderGoods order, HttpSession session) {
		Json j = new Json();
		try {
			OrderGoods result = orderGoodsService.saveGoods(order);
			j.setSuccess(true);
			j.setMsg("修改成功！");
			logUserAction(session, "/orderGoodsController/edit", result.toString());
		} catch (Exception e) {
			j.setMsg("修改失败：" + e.getMessage());
		}
		return j;
	}


	@RequestMapping("/save_or_submit")
	@ResponseBody
	public Json edit_ok(OrderGoods order, HttpSession session) {
		Json j = new Json();
		try {
			OrderGoods result = orderGoodsService.edit(order);
			j.setSuccess(true);
			j.setMsg("修改成功！");
			logUserAction(session, "/orderGoodsController/save_or_submit", result.toString());
		} catch (Exception e) {
			j.setMsg("修改失败：" + e.getMessage());
		}
		return j;
	}

	@RequestMapping("/cancel")
	@ResponseBody
	public Json cancel(OrderGoods order, HttpSession session) {
		Json j = new Json();
		try {
			OrderGoods result = orderGoodsService.editGoodsStatus(order);
			j.setSuccess(true);
			j.setMsg("取消成功！");
			logUserAction(session, "/orderGoodsController/cancel", result.toString());
		} catch (Exception e) {
			j.setMsg("取消失败：" + e.getMessage());
		}
		return j;
	}



	@RequestMapping("/agencies")
	@ResponseBody
	public Json getAgenciesByType(String goodsType) {
		System.out.println(goodsType);
		Json j = new Json();
		try {
			Map<String, String> result = orderGoodsService.getAgenciesByType(goodsType);
			j.setSuccess(true);
			j.setObj(result);		
		} catch (Exception e) {
			j.setMsg("根据商品类型得到供货商列表失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/goods")
	@ResponseBody
	public Json getGoodsByAgencyId(String agencyId) {
		System.out.println(agencyId);
		Json j = new Json();
		try {
			Map<String, String> result = orderGoodsService.getGoodsByAgencyId(agencyId);
			j.setSuccess(true);
			j.setObj(result);		
		} catch (Exception e) {
			j.setMsg("根据供货商得到商品列表失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/addresses")
	@ResponseBody
	public Json getAddressesByCardId(String cardId) {
		System.out.println(cardId);
		Json j = new Json();
		try {
			Map<String, String> result = orderGoodsService.getAddressesByCardId(cardId);
			j.setSuccess(true);
			j.setObj(result);		
		} catch (Exception e) {
			j.setMsg("根据卡号得到地址列表失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/addDetails")
	@ResponseBody
	public Json addDetails(OrderGoods order, HttpSession session) {
		Json j = new Json();
		try {
			OrderGoods result = orderGoodsService.addDetails(order);
			j.setSuccess(true);
			j.setMsg("生成详单成功！");
			//j.setObj(result);
			
			//add log
			logUserAction(session, "/orderGoodsController/addDetails", result.toString());
			
		} catch (Exception e) {
			j.setMsg("生成详单失败：" + e.getMessage());
		}
		return j;
	}

}
